//
//  HomeViewController.swift
//  JFDouYu
//
//  Created by xujf on 2017/11/13.
//  Copyright © 2017年 XuJF. All rights reserved.
//

import UIKit
private let kPageTitleViewH : CGFloat = 40
class HomeViewController: UIViewController {
  
    fileprivate lazy var pageTitleView : PageTitleView = {[weak self] in  //闭包创建懒加载 容易产生循环引用使用  [weak self] in
    let titleFrame = CGRect(x: 0, y: kStatusBarH + kNavigetionH, width: kScreenW, height: kPageTitleViewH)
        let titles :[String] = ["推荐", "游戏","娱乐","趣玩"]
        let titleView = PageTitleView(frame: titleFrame, titles: titles)
        titleView.backgroundColor = UIColor.white
        titleView.delegate = self
        return titleView
    }()
    fileprivate lazy var pageContentView : PageContentView = { [weak self] in
        let frame = CGRect(x: 0, y: kNavigetionH + kStatusBarH + kPageTitleViewH, width: kScreenW, height:kScreenH - kStatusBarH - kPageTitleViewH - kNavigetionH - kTabbarH)
        var childVcs = [UIViewController]()
        childVcs.append(ReconmandViewController())
        for _ in 0..<3 {
            let vc = UIViewController()
            childVcs.append(vc)
        }
        
        let pageContentView = PageContentView(frame: frame, childVcs: childVcs, parentViewController: self)
        pageContentView.delegate = self
        return pageContentView
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        // Do any additional setup after loading the view.
    }

   
    
}
extension HomeViewController {
    private func setUI(){
        automaticallyAdjustsScrollViewInsets = true
        setNaviegationBar()
        view.addSubview(pageTitleView)
        view.addSubview(pageContentView)
    }
    private func setNaviegationBar(){
        let btn = UIButton()
//        UIImage(named: <#T##String#>)b
        btn.setImage(UIImage(named: "logo"), for: UIControlState.normal)
        btn.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        let historyBtn = UIButton()
        //        UIImage(named: <#T##String#>)b
        historyBtn.setImage(#imageLiteral(resourceName: "image_my_history"), for: UIControlState.normal)
        historyBtn.setImage( #imageLiteral(resourceName: "Image_my_history_click"), for: .highlighted)
        historyBtn.sizeToFit()
        
        let historyItem = UIBarButtonItem(customView: historyBtn)
        navigationItem.rightBarButtonItem = historyItem
        let size = CGSize(width: 40, height: 40)
        
        let searchItem = UIBarButtonItem.creatImge(imageName:"image_my_history", highImageName: "image_my_history", size: size)
        let scanItem = UIBarButtonItem(imageName: "image_my_history", highImageName: "image_my_history", size: size)
        
        navigationItem.rightBarButtonItems = [historyItem,searchItem,scanItem]
        
        
    }
//    fileprivate func setupTitleView{
//
//    }
    
}

//MARK:-遵守协议
extension HomeViewController : PageTitleViewDelegate{
    func pageTitleView(_ titleView: PageTitleView, selectedIndex index: Int) {
        pageContentView.setCurrentIndex(currentIndex: index)
    }
    
}
extension HomeViewController : PageContentViewDelegate{
    func pageContentView(pageContetenView: PageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        print("HomeViewController \(progress)","\(sourceIndex)","\(targetIndex)")
        self.pageTitleView.setTitleViewProgress(progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}
